<template>
  <el-card shadow="never">
    <template #header>
      <div class="card-header">
        <div>
          <el-button type="primary" :icon="Plus" @click="addDialog">新增</el-button>
        </div>
      </div>
    </template>
    <el-table v-loading="loading" :data="tableData" style="width: 100%" height="500"
      :default-sort="{ prop: 'date', order: 'descending' }"
              @selection-change="handleSelectionChange"
      row-key="deptName" :default-expand-all="true">
      <!-- <el-table-column fixed type="selection" width="55"/> -->
      <!-- <el-table-column prop="id" label="单位编号" width="60"/> -->
      <el-table-column prop="deptName" label="单位名称" width="180" show-overflow-tooltip/>
      <el-table-column prop="deptAlias" label="单位别称"  show-overflow-tooltip/>
      <el-table-column prop="deptIcon" label="图标"  show-overflow-tooltip>
        <template #default="scope">
          <el-image
              style="width: 50px; height: 50px"
              :src="scope.row.deptIcon"
              :zoom-rate="1.2"
              :initial-index="0"
              fit="cover"/>
        </template>
      </el-table-column>

      <el-table-column prop="principal" label="负责人"  show-overflow-tooltip/>
      <el-table-column prop="principalPhone" label="负责人手机"  show-overflow-tooltip/>
<!--      <el-table-column prop="deptIntro" label="单位简介" width="120" show-overflow-tooltip/>-->
      <!-- <el-table-column prop="parentId" label="上级单位" width="120" /> -->
<!--      <el-table-column prop="provinceName" label="省"  show-overflow-tooltip/>-->
<!--      <el-table-column prop="cityName" label="市"  show-overflow-tooltip/>-->
<!--      <el-table-column prop="districtName" label="区" show-overflow-tooltip/>-->
<!--      <el-table-column prop="address" label="详细地址" show-overflow-tooltip/>-->
<!--      <el-table-column prop="remark" label="备注" width="120" show-overflow-tooltip/>-->
      <el-table-column fixed="right" label="操作" width="220">
        <template #header>
          <el-input v-model="params.search" @keyup.enter.native="searchData" placeholder="搜索单位名称" />
        </template>
        <template #default="scope">
          <el-button link type="danger" :icon="Delete" size="small" @click="showDeleteDialog(scope.row.id)">删除
          </el-button>
          <el-button link type="primary" :icon="Edit" size="small" @click="editDialog(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination v-model:currentPage="params.current" v-model:page-size="params.size"
      :page-sizes="[100, 200, 300, 400]" layout="sizes, prev, pager, next" :total="tableData.total"
      @size-change="handleSizeChange" @current-change="handleCurrentChange" />
  </el-card>

  <!-- 新增或者修改页面-->
    <dept-add-or-edit @closeEditDialog="closeEditDialog" :addOrEditVisible="addOrEditVisible"
                   :ruleForm="ruleForm"
                   :addDataType="addDataType">

    </dept-add-or-edit>
</template>

<script lang="ts" setup>
import {ElTable, ElMessage, ElMessageBox} from 'element-plus'
import type { TableColumnCtx } from 'element-plus/es/components/table/src/table-column/defaults'
import { onMounted, computed, reactive, ref } from 'vue'
import { Params } from "@/apis/model/common/common.interfaces"
import { Delete, Edit, Search, Share, Upload, Plus } from '@element-plus/icons-vue'
import { Dept } from "@/apis/model/system/DeptModel";
import { deleteDeptById, deptLoop, deptPage } from '@/apis/http/system/DeptHttp'
import DeptAddOrEdit from "@/views/system/dept/DeptAddOrEdit.vue";

const params: Params = reactive({
  current: 1,
  size: 10,
  search: '',
  entity: {} as Dept
})

const ruleForm = ref<Dept>({} as Dept)
// AddOrEditVisible
const addOrEditVisible = ref(false)
// table 表数据
const tableData: any = ref<Array<Dept>>([])
// 加载数据
const loading = ref(true)
const addDataType = ref('add');

/**
 * 初始化数据
 */
const initList = () => {
  loading.value = true
  deptLoop().then(res => {
    const code = res.code
    const message = res.message
    const data = res.data
    if (code == 200) {
      tableData.value = data
    }
    loading.value = false
  })
}

/**
 * search 查询数据
 */
const searchData = () => {
  initList()
}

/**
 * 每页数据量改变查询数据
 * @param val
 */
const handleSizeChange = (val: number) => {
  params.size = val
  initList()
}

/**
 * 分页查询数据
 * @param val
 */
const handleCurrentChange = (val: number) => {
  params.current = val
  initList()
}

/**
 * 添加弹出
 */
const addDialog = () => {
  addOrEditVisible.value = true
  ruleForm.value = {} as Dept
  addDataType.value = 'add'
}

/**
 * 修改弹出页面
 */
const editDialog = (row: Dept) => {
  addOrEditVisible.value = true
  ruleForm.value = row
  addDataType.value = 'edit'
}

const closeEditDialog = (e: any) => {
  addOrEditVisible.value = e.addOrEditVisible
  if (e.refushList = true) {
    params.current = 1;
    initList();
  }
}

/**
 * 弹出删除对话框
 */
const showDeleteDialog = (id: number) => {
  ElMessageBox.confirm(
      '是否删除角色?',
      '提醒',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        deleteDeptById(id).then(res => {
          const code = res.code;
          if (code == 200) {
            ElMessage({
              message: '删除数据成功！',
              type: 'success',
            })
            initList();
          } else {
            ElMessage.error('删除数据失败！');
          }
        })
      })
      .catch(() => {
        // ElMessage({
        //   type: 'info',
        //   message: 'Delete canceled',
        // })
      })
}

onMounted(() => {
  initList()
})
</script>
